package cn.wolfcode.luowowo.mgrsite.listener;


import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/*
 * 从mysql中查询数据同步到redis中
 * redis数据预热*/

//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("---------------begin---------------------");
        List<StrategyDetail> list = strategyDetailService.list();
        //StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            //如果vo对象的key已经存在,跳过,不需要在进行初始化操作
            if(strategyStatisRedisService.isStrategyStatisVOExsit(detail.getId())){
                continue;//跳过
            }

            //拷贝相同的属性名
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail, vo);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            strategyStatisRedisService.setStrategyStatisVo(vo);
        }
        System.out.println("---------------vo初始化---------------------");

        //热门排行初始化
        for (StrategyDetail detail : list) {
            String viId = RedisKeys.STRATEGY_TYPE_VIEWNUM.join(detail.getId().toString());
            String prefix = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            //判断某个void是否存在指定的zset中
            if (strategyStatisRedisService.isVoidExsitZset(viId,prefix)){
                continue;
            }
            //浏览+评论数
            int score=detail.getViewnum()+detail.getReplynum();
            strategyStatisRedisService.addHotScores(detail.getId(),score);
            }
        //热门排行初始化
        for (StrategyDetail detail : list) {
            String viId = RedisKeys.STRATEGY_TYPE_VIEWNUM.join(detail.getId().toString());
            String prefix = null;
            //判断某个void是否存在指定的zset中
            if (detail.isIsabroad()){
                //国外
                prefix= RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();;
            }else {
                //国内
                prefix= RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();;
            }
            //浏览+评论数
            int score=detail.getViewnum()+detail.getReplynum();
            strategyStatisRedisService.addScoreAbroadAndChina(detail.getId(),score);
            }
    }
}